﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using qYun.Shared.Models;
using qYun.ApiService.Services;

namespace qYun.ApiService.Controllers;

/// <summary>
/// 
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class DemoController : ControllerBase
{
    private readonly ILogger<DemoController> _logger;
    private readonly IHubContext<DataHub> _hubContext;
    private readonly IDbService _dbService;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="logger"></param>
    public DemoController(ILogger<DemoController> logger,IHubContext<DataHub> hubContext,IDbService dbService)
    {
        _logger = logger;
        _hubContext = hubContext;
        _dbService = dbService;
    }

    /// <summary>
    /// 获取示例数据
    /// </summary>
    /// <returns>示例响应</returns>
    [HttpGet]
    public IActionResult Get()
    {
        return Ok(new { message = "Hello, Swagger!" });
    }
    
    [HttpGet("items")]
    public async Task<ActionResult<IEnumerable<Item>>> GetItems()
    {
        return Ok(new List<Item> { new Item { Id = 1, Name = "Item 1" } });
    }

    [HttpGet("update")]
    public async Task<IActionResult> UpdateData()
    {
        var res =await _dbService.GetGasDataAsync();
        var data =res.LastOrDefault();
        
       // await _hubContext.Clients.All.SendAsync("UpdateData", data);
        return Ok(data);
    }
}

/// <summary>
/// 
/// </summary>
public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}